#include "AsynchIO.h"
#include "Proactor.h"
#include "AsynchIOImpl.h"
#include "Trace.h"

AsynchResult::AsynchResult (AsynchResultImpl *impl)
: implementation_ (impl)
{
    TRACE ("");
}
AsynchResult::~AsynchResult ()
{
    TRACE ("");
}
size_t AsynchResult::bytes_transferred (void) const
{
    return this->implementation_->bytes_transferred ();
}
int AsynchResult::success (void) const
{
    return this->implementation_->success ();
}
const void *AsynchResult::completion_key (void) const
{
    return this->implementation_->completion_key ();
}
int AsynchResult::error () const
{
    return this->implementation_->error ();
}
long long AsynchResult::offset () const
{
    return this->implementation_->offset ();
}
AsynchResultImpl *AsynchResult::implementation () const
{
    return this->implementation_;
}
// -----------------------------------------------------------------------
AsynchOpt::AsynchOpt ()
{
    TRACE ("");
}
AsynchOpt::~AsynchOpt ()
{
    TRACE ("");
}
int AsynchOpt::open (const AsynchHandler *handler,
	NDK_HANDLE handle,
	const void *completion_key, 
	Proactor *proactor)
{
    TRACE ("");
    return this->implementation ()->open (handler,
            handle,
            completion_key,
            proactor);
}
int AsynchOpt::cancel ()
{
    TRACE ("");
    if (0 == this->implementation ())
        return -1;
    return this->implementation ()->cancel ();
}
Proactor *AsynchOpt::proactor () const
{
    if (0 == this->implementation ())
        return 0;
    return this->implementation ()->proactor ();
}
// -----------------------------------------------------------------------
AsynchReadStream::AsynchReadStream ()
: implementation_ (0)
{
    TRACE ("");
}
AsynchReadStream::~AsynchReadStream ()
{
    TRACE ("");
    if (this->implementation_)
        delete this->implementation_;
    this->implementation_ = 0;
}
int AsynchReadStream::open (const AsynchHandler *handler,
        NDK_HANDLE handle/* = NDK_INVALID_HANDLE*/,
        const void *completion_key/* = 0*/,
        Proactor *proactor/* = 0*/)
{
    TRACE ("");
    if (proactor == 0)
    {
        proactor = handler->proactor ();
        if (proactor == 0)
            proactor = Proactor::instance ();
    }

    // Now let us get the implementation initialized.
    if ((this->implementation_ = proactor->create_asynch_read_stream ()) == 0)
        return -1;
    // Call the <open> method of the base class.
    return AsynchOpt::open (handler,
            handle,
            completion_key,
            proactor);
}
int AsynchReadStream::read (MessageBlock *message_block,
        size_t num_bytes_to_read)
{
    TRACE ("");
    if (0 == this->implementation_)
        return -1;
    return this->implementation_->read (message_block,
            num_bytes_to_read);
}
AsynchOptImpl *AsynchReadStream::implementation () const
{
    return this->implementation_;
}
AsynchReadStream::Result::Result (AsynchReadStreamResultImpl *impl)
: AsynchResult (impl)
, implementation_ (impl)
{
    TRACE ("");
}
AsynchReadStream::Result::~Result ()
{
    TRACE ("");
}
size_t AsynchReadStream::Result::bytes_to_read () const
{
    return this->implementation ()->bytes_to_read ();
}
MessageBlock *AsynchReadStream::Result::message_block () const
{
    return this->implementation ()->message_block ();
}
NDK_HANDLE AsynchReadStream::Result::handle () const
{
    return this->implementation ()->handle ();
}
AsynchReadStreamResultImpl *AsynchReadStream::Result::implementation () const
{
    return this->implementation_;
}
// ------------------------------------------------------------------------
AsynchWriteStream::AsynchWriteStream ()
: implementation_ (0)
{
}
AsynchWriteStream::~AsynchWriteStream ()
{
    TRACE ("");
    if (this->implementation_)
        delete this->implementation_;
    this->implementation_ = 0;
}
int AsynchWriteStream::open (const AsynchHandler *handler,
        NDK_HANDLE handle/* = NDK_INVALID_HANDLE*/,
        const void *completion_key/* = 0*/,
        Proactor *proactor/* = 0*/)
{
    TRACE ("");
    if (proactor == 0)
    {
        proactor = handler->proactor ();
        if (proactor == 0)
            proactor = Proactor::instance ();
    }

    // Now let us get the implementation initialized.
    if ((this->implementation_ = proactor->create_asynch_write_stream ()) == 0)
        return -1;
    // Call the <open> method of the base class.
    return AsynchOpt::open (handler,
            handle,
            completion_key,
            proactor);
}
int AsynchWriteStream::write (MessageBlock *message_block,
        size_t num_bytes_to_write)
{
    TRACE ("");
    if (0 == this->implementation_)
        return -1;
    return this->implementation_->write (message_block,
            num_bytes_to_write);
}
AsynchOptImpl *AsynchWriteStream::implementation () const
{
    return this->implementation_;
}
AsynchWriteStream::Result::Result (AsynchWriteStreamResultImpl *impl)
: AsynchResult (impl)
, implementation_ (impl)
{
}
AsynchWriteStream::Result::~Result ()
{
}
size_t AsynchWriteStream::Result::bytes_to_write () const
{
    return this->implementation ()->bytes_to_write ();
}
MessageBlock *AsynchWriteStream::Result::message_block () const
{
    return this->implementation ()->message_block ();
}
NDK_HANDLE AsynchWriteStream::Result::handle () const
{
    return this->implementation ()->handle ();
}
AsynchWriteStreamResultImpl *AsynchWriteStream::Result::implementation () const
{
    return this->implementation_;
}
// ------------------------------------------------------------------------
AsynchAccept::AsynchAccept ()
: implementation_ (0)
{
}
AsynchAccept::~AsynchAccept ()
{
    TRACE ("");
    if (this->implementation_)
        delete this->implementation_;
    this->implementation_ = 0;
}
int AsynchAccept::open (const AsynchHandler *handler,
        NDK_HANDLE handle/* = NDK_INVALID_HANDLE*/,
        const void *completion_key/* = 0*/,
        Proactor *proactor/* = 0*/)
{
    TRACE ("");
    if (proactor == 0)
    {
        proactor = handler->proactor ();
        if (proactor == 0)
            proactor = Proactor::instance ();
    }

    // Now let us get the implementation initialized.
    if ((this->implementation_ = proactor->create_asynch_accept ()) == 0)
        return -1;
    // Call the <open> method of the base class.
    return AsynchOpt::open (handler,
            handle,
            completion_key,
            proactor);
}
int AsynchAccept::accept (MessageBlock *message_block,
        size_t num_bytes_to_read)
{
    TRACE ("");
    if (0 == this->implementation_)
        return -1;
    return this->implementation_->accept (message_block,
            num_bytes_to_read); 
}
AsynchOptImpl *AsynchAccept::implementation () const
{
    return this->implementation_;
}
AsynchAccept::Result::Result (AsynchAcceptResultImpl *impl)
: AsynchResult (impl)
, implementation_ (impl)
{
    TRACE ("");
}
AsynchAccept::Result::~Result ()
{
    TRACE ("");
}
size_t AsynchAccept::Result::num_to_accept () const
{
    return this->implementation ()->num_to_accept ();
}
MessageBlock *AsynchAccept::Result::message_block () const
{
    return this->implementation ()->message_block ();
}
NDK_HANDLE AsynchAccept::Result::listen_handle () const
{
    return this->implementation ()->listen_handle ();
}
/*
NDK_HANDLE AsynchAccept::Result::accept_handle () const
{
    return this->implementation ()->accept_handle ();
}
*/
AsynchAcceptResultImpl *AsynchAccept::Result::implementation () const
{
    return this->implementation_;
}
// -----------------------------------------------------------------------------
AsynchHandler::AsynchHandler ()
: proactor_ (0)
, handle_ (NDK_INVALID_HANDLE)
{
    TRACE ("");
}
AsynchHandler::AsynchHandler (Proactor *p)
: proactor_ (p)
, handle_ (NDK_INVALID_HANDLE)
{
    TRACE ("");
}
AsynchHandler::~AsynchHandler ()
{
    TRACE ("");
}
int AsynchHandler::open (NDK_HANDLE )
{
    TRACE ("");
    return -1;
}
int AsynchHandler::handle_read_stream (const AsynchReadStream::Result &/*result*/)
{
    TRACE ("");
    return -1;
}
int AsynchHandler::handle_write_stream (const AsynchWriteStream::Result &/*result*/)
{
    TRACE ("");
    return -1;
}
#if 0
void AsynchHandler::handle_read_file (const AsynchReadFile::Result &/*result*/)
{
}
void AsynchHandler::handle_write_file (const AsynchWriteFile::Result &/*result*/)
{
}
#endif 
int AsynchHandler::handle_accept (const AsynchAccept::Result &/*result*/)
{
    TRACE ("");
    return -1;
}
#if 0
void AsynchHandler::handle_connect (const ACE_Asynch_Connect::Result &/*result*/)
{
}
#endif
int AsynchHandler::handle_close (const int )
{
    TRACE ("");
    return 0;
}
Proactor *AsynchHandler::proactor (void) const
{
    return this->proactor_;
}
void AsynchHandler::proactor (Proactor *p)
{
    this->proactor_ = p;
}
NDK_HANDLE AsynchHandler::handle (void) const
{
    return this->handle_;
}
void AsynchHandler::handle (NDK_HANDLE handle) 
{
    this->handle_ = handle;
}
void AsynchHandler::remote_addr (const InetAddr &)
{
}

